/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TConfig;
import com.ruipengkj.bean.TStore;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.IDNameBean;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.JPanelFlowLayoutComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonWhite;
import com.ruipengkj.service.TConfigService;
import com.ruipengkj.service.TStoreService;
import com.ruipengkj.view.base.BasePanel;

/**
 * 说明：
 * 系统配置设置
 * @Author：黄盛强
 * @Date：2012-03-23
 */
public class SystemSetAllocation extends BasePanel implements Runnable{
	
	private static final long serialVersionUID = 1L;
	
	private JLabelJComboBoxComponent setStock; //库存报警设置 
//	private JLabelJComboBoxComponent setPrice;//当开出发票时是否按税率重新计算价格  【是/否】
//	private JLabelJTextFieldComponent setTaxRate;//税率设置 【输入域，单位为%】 
//	private JLabelJTextFieldComponent clentTF;//客户端标示
	private JButton sureButton;//信息确认
	private JButton selectButton;
	
	private JLabelJComboBoxComponent barCodePrint; //条码打印机
	private JLabelJComboBoxComponent smallPrint; //小票打印机
	
	private JLabelJComboBoxComponent sellStoreName;//销售仓库名称
	
	private JLabelJTextFieldComponent printNum;//打印份数
	private JLabelJTextFieldComponent sqlPath;//打印份数
	private TConfigService tConfigService;
	private TStoreService tStoreService;//仓库
	public SystemSetAllocation() {
		tConfigService = BuildSpringApplication.getInstance().getTConfigServiceImpl();
		tStoreService = BuildSpringApplication.getInstance().getTStoreServiceImpl();
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.init();
		SwingUtilities.invokeLater(this);
	}
	
	private void init() {
		JPanel main_jp = uiFactory.createBoxJPanel(BoxLayout.Y_AXIS);
		main_jp.setBorder(BorderFactory.createTitledBorder("系统配置信息"));
		
		setStock = new JLabelJComboBoxComponent("                      库存报警设置：", new String[]{
				"不报警", "报警下阀值", "报警上阀值", "全部报警"
		}, 188);
//		setTaxRate = new JLabelJTextFieldComponent("              税率设置 【单位为%】：", 30);
//		setPrice = new JLabelJComboBoxComponent("当开出发票时是否按税率重新计算价格：", new String[]{"是", "否"}, 188);
		
		barCodePrint=new JLabelJComboBoxComponent("                        条码打印机：", new String[]{"不打印"},188);
		smallPrint=new JLabelJComboBoxComponent("                        小票打印机：", new String[]{"不打印"},188);
		sellStoreName=new JLabelJComboBoxComponent("                      默认销售仓库：", new String[]{},188);
		printNum = new JLabelJTextFieldComponent("                      小票打印份数：", 30);
		sqlPath = new JLabelJTextFieldComponent("                    数据库安装目录：", 30);
		selectButton = new RPJButtonWhite("选择");
		selectButton.addActionListener(this);
		sqlPath.add(selectButton);
		main_jp.add(setStock);
//		main_jp.add(setTaxRate);
//		main_jp.add(setPrice);
		main_jp.add(barCodePrint);
		main_jp.add(smallPrint);
		main_jp.add(sellStoreName);
		main_jp.add(printNum);
		main_jp.add(sqlPath);
		
		sureButton = new RPJButtonBlue("确定");
		sureButton.addActionListener(this);
		JPanel jp4 = new JPanelFlowLayoutComponent(sureButton, FlowLayout.CENTER);
		main_jp.add(jp4);
		this.add(main_jp);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectButton) {
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fileChooser.showOpenDialog(null);
			sqlPath.getText().setText(fileChooser.getSelectedFile().getPath());
			return;
		}
//		String taxRate = setTaxRate.getText().getText().trim();//记录税收
//		if(taxRate != null && !taxRate.trim().equals("") && (!MatcherFormatUtil.MatcherNumberScheme(taxRate.toString()) || taxRate.toString().length() >= 10)){
//			Util.showMessageDialog("税率只能为整数，且不能大于10位");
//			return;
//		}
		String printNum = this.printNum.getText().getText().trim();//打印份数
		if (printNum == null || printNum.trim().equals("")) {
			Util.showMessageDialog("小票打印份数不能为空");
			return;
		}
		if((!MatcherFormatUtil.MatcherNumberScheme(printNum.toString()) || printNum.toString().length() >= 6)){
			Util.showMessageDialog("小票打印份数只能为整数，且不能大于6位");
			return;
		}
		TConfig tConfig = new TConfig();//新数据
		tConfig.setAlarm((short)setStock.getBox().getSelectedIndex());
//		tConfig.setReCalTaxPrice(setPrice.getBox().getSelectedIndex() == 0 ? true : false);
		if(barCodePrint.getBox().getSelectedIndex()>0){
			tConfig.setBarCodePrintName(barCodePrint.getBox().getSelectedItem().toString());
		}
		if(smallPrint.getBox().getSelectedIndex()>0){
			tConfig.setSmallPrintName(smallPrint.getBox().getSelectedItem().toString());
		} else {
			tConfig.setSmallPrintName("");//表示不打印
		}
		if(sellStoreName.getBox().getSelectedIndex()>0){
			IDNameBean bean=(IDNameBean)sellStoreName.getBox().getSelectedItem();
			tConfig.setSellStoreID(bean.getStrId());
		}
		tConfig.setPrintNum(Integer.parseInt(printNum));
		tConfig.setSqlPath(sqlPath.getText().getText().trim());
//		if (taxRate!= null && !taxRate.equals(""))
//			tConfig.setTaxRate(Integer.parseInt(taxRate));
		try {
			List<TConfig> tConfigList = tConfigService.findService(null);
			if (tConfigList != null && tConfigList.size() != 0) {
				tConfig.setId(tConfigList.get(0).getId());
			}
			tConfigService.saveOrUpdateService(tConfig);//更新新数据
			BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("保存系统配置信息");
			Util.showMessageDialog("保存成功！");
		} catch (Exception e1) {
			e1.printStackTrace();
			Util.showMessageDialog("保存失败！");
		}
	}

	@Override
	public void run() {
		barCodePrint.getModel().removeAllElements();
		smallPrint.getModel().removeAllElements();
		
		barCodePrint.addElement("不打印");
		smallPrint.addElement("不打印");
		//添加打印机列表
		List<String> printList=Util.getAllPrinter();
		if(printList!=null&&printList.size()!=0){
			for(int i=0;i<printList.size();i++){
				barCodePrint.addElement(printList.get(i));
				smallPrint.addElement(printList.get(i));
			}
		}
		TConfig comfig=null;
		try {
			//将数据库中的数据显示出来
			List<TConfig> tConfigList = tConfigService.findService(null);
			if (tConfigList != null && tConfigList.size() != 0) {
				comfig=tConfigList.get(0);
				setStock.getBox().setSelectedIndex(comfig.getAlarm());
//				setTaxRate.getText().setText(String.valueOf(comfig.getTaxRate()));
//				setPrice.getBox().setSelectedIndex(comfig.isReCalTaxPrice() ? 0 : 1);
				//条码打印机
				if (comfig.getBarCodePrintName() == null || comfig.getBarCodePrintName().equals(""))
					barCodePrint.getBox().setSelectedIndex(0);
				else
					barCodePrint.getBox().setSelectedItem(comfig.getBarCodePrintName());
				//小票打印机
				if (comfig.getSmallPrintName() == null || comfig.getSmallPrintName().equals(""))
					smallPrint.getBox().setSelectedIndex(0);
				else
					smallPrint.getBox().setSelectedItem(comfig.getSmallPrintName());
				printNum.getText().setText(comfig.getPrintNum()+"");
				sqlPath.getText().setText(comfig.getSqlPath());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//清苦信息
		sellStoreName.getModel().removeAllElements();
		sellStoreName.addElement(new IDNameBean("0", "请选择"));
		//显示所有仓库信息
		try {
			List<TStore> tStoreList=tStoreService.findService(null);
			if(tStoreList!=null&&tStoreList.size()!=0){
				for(int i=0;i<tStoreList.size();i++){
					TStore store=tStoreList.get(i);
					sellStoreName.addElement(new IDNameBean(store.getStoreID(), store.getName()));
					if(comfig!=null&&comfig.getSellStoreID()!=null){
						if(comfig.getSellStoreID().equals(store.getStoreID())){
							sellStoreName.getBox().setSelectedIndex(i+1);
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void clear() {
		SwingUtilities.invokeLater(this);
	}
}
